﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using DAL;
namespace BAL
{
    public class BAL_DICHVU
    {
        private static QuanLyTiemNetDataContext db = new QuanLyTiemNetDataContext();
        //Them
        public static bool ThemDV(ref string err,string MaDV,string TenDV,string DVT,float DonGia)
        {
            bool them = false;
            try
            {
                DICHVU dv = new DICHVU();
                dv.MaDV = MaDV;
                dv.TenDV = TenDV;
                dv.DVT = DVT;
                dv.DonGia = DonGia;
                dv.DaXoa = false;
                db.DICHVUs.InsertOnSubmit(dv);
                db.SubmitChanges();
                them = true;
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return them;
        }
        //Cập Nhật
        public static bool CapNhatDV(ref string err, string MaDV, string TenDV, string DVT, float DonGia)
        {
            bool update = false;
            try
            {
                DICHVU tim = db.DICHVUs.Single(dv => dv.MaDV.Equals(MaDV));
                if(tim!=null)
                {
                    tim.TenDV = TenDV;
                    tim.DVT = DVT;
                    tim.DonGia = DonGia;
                    db.SubmitChanges();
                    update = true;

                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return update;
        }
        //Xóa
        public static bool XoaDV(ref string err,string MaDV)
        {
            bool xoa = false;
            try
            {
                DICHVU tim = db.DICHVUs.Single(dv => dv.MaDV.Equals(MaDV));
                if (tim != null)
                {
                    db.DICHVUs.DeleteOnSubmit(tim);
                    db.SubmitChanges();
                    xoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return xoa;
        }

        //Đánh dấu xóa

        public static bool DanhDauXoa(ref string err,string MaDV,bool value)
        {
            bool danhdauxoa = false;
            try
            {
                DICHVU tim = db.DICHVUs.Single(dv => dv.MaDV.Equals(MaDV));
                if (tim != null)
                {
                    tim.DaXoa = value;
                    db.SubmitChanges();
                    danhdauxoa = true;
                }
            }
            catch(Exception e)
            {
                err = e.Message;
            }
            return danhdauxoa;
        }
        //ConverToDataTable
        public static DataTable ConverToDataTable(List<DICHVU> list)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("MaDV");
            dt.Columns.Add("TenDV");
            dt.Columns.Add("DVT");
            dt.Columns.Add("DonGia");
            //dt.Columns.Add("DaXoa");
            foreach(var i in list)
            {
                dt.Rows.Add(i.MaDV, i.TenDV, i.DVT, i.DonGia);
            }
            return dt;

        }
        //lấy thông tin
        public static DataTable LayThongTinDV()
        {
            var ttdv = from dv in db.DICHVUs where dv.DaXoa==false select dv;
            return ConverToDataTable(ttdv.ToList());
        }
        //lấy lịch sử xóa
        public static DataTable LayLichSuDV()
        {
            var ttdv = from dv in db.DICHVUs where dv.DaXoa == true select dv;
            return ConverToDataTable(ttdv.ToList());
        }
        //Lấy Giá Dịch Vụ
        public static float LayGiaDichVu(string MaDV)
        {
            DICHVU dv=new DICHVU();
            float gia=0;
            try
            {
                 dv = db.DICHVUs.Single(mdv => mdv.MaDV.Equals(MaDV));
                gia= float.Parse(dv.DonGia.ToString());
            }
            catch(Exception)
            {
                
            }
            return gia;
            
        }
    }

    

   
}
